using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace YEngineLib.Util
{
    public class YText
    {
        public enum TextAlignment { Left, Center, Right, None };

        public SpriteFont Font;
        public Color TextColor;
        public string Text;

        public TextAlignment Alignment;

        public Color ShadowColor;
        public Vector2 ShadowOffset;
        public bool isShadowed;

        public int Alpha
        {
            get;
            set;
        }

        public static Color GetColorAlpha(Color noAlpha, int alpha)
        {
            return Color.FromNonPremultiplied(noAlpha.R, noAlpha.G, noAlpha.B, alpha);
        }

        public YText(SpriteFont font, string text)
        {
            Alpha = 255;
            isShadowed = true;
            Alignment = TextAlignment.Center;
            //isCentered = true;
            this.Font = font;
            this.Text = text;

            TextColor = Color.White;

            ShadowColor = Color.Black;
            ShadowOffset = new Vector2(4.0f, 4.0f);
        }

        public void Draw(SpriteBatch spriteBatch, Vector2 position, float scale)
        {
            if (Text == null)
                return;

            Vector2 offset = Font.MeasureString(Text);

            offset.Y /= 2.0f;

            switch (Alignment)
            {
                case TextAlignment.None:
                    {
                        offset = Vector2.Zero;
                    }
                    break;
                case TextAlignment.Left:
                    {
                        offset.X = 0.0f;// = Vector2.Zero;
                    }
                    break;
                case TextAlignment.Center:
                    {
                        offset.X /= 2.0f;
                    }
                    break;
                //case TextAlignment.Right:
                //    {
                //        //offset = font.MeasureString(text);

                //    }
                //    break;
            }
            //  Draw shadow
            if (isShadowed)
                spriteBatch.DrawString(Font, Text, position + (ShadowOffset * scale), GetColorAlpha(ShadowColor, Alpha), 0.0f, offset, scale, 0, 0);

            spriteBatch.DrawString(Font, Text, position, GetColorAlpha(TextColor, Alpha), 0.0f, offset, scale, 0, 0);
        }

        ///////////////////////////////////////////////////////////////////////
        public static void Draw(SpriteBatch spriteBatch, SpriteFont font, string text, Vector2 position, float scale,
               Color textColor, TextAlignment alignment)
        {
            Draw(spriteBatch, font, text, position, scale, textColor, Color.Black, new Vector2(4.0f, 4.0f), 255, alignment);
        }

        public static void Draw(SpriteBatch spriteBatch, SpriteFont font, string text, Vector2 position, float scale,
                Color textColor, Color shadowColor, Vector2 shadowOffset, int Alpha, TextAlignment alignment)
        {
            Vector2 offset = font.MeasureString(text);

            offset.Y /= 2.0f;

            switch (alignment)
            {
                case TextAlignment.None:
                    {
                        offset = Vector2.Zero;
                    }
                    break;
                case TextAlignment.Left:
                    {
                        offset.X = 0.0f;
                    }
                    break;
                case TextAlignment.Center:
                    {
                        offset.X /= 2.0f;
                    }
                    break;
            }
            //  Draw shadow
            spriteBatch.DrawString(font, text, position + (shadowOffset * scale), GetColorAlpha(shadowColor, Alpha), 0.0f, offset, scale, 0, 0);
            spriteBatch.DrawString(font, text, position, GetColorAlpha(textColor, Alpha), 0.0f, offset, scale, 0, 0);
        }
    }
}
